import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './user.entity';
import { Logs } from 'src/logs/logs.entity';



@Injectable()
export class UserService {
    constructor(
        @InjectRepository(User)
        private userRepository: Repository<User>,

        @InjectRepository(Logs)
        private logsRepository: Repository<Logs>,
    ) {}

    
    findAll() {
        return this.userRepository.find();
    }
    find(username: string) {
        return this.userRepository.findOne({
            where: {
                username,
            }
        });
    }

    findById(id: number) {
        return this.userRepository.findOne({
            where: {
                id,
            }
        });
    }
    findProfile(userId: number) {
        return this.userRepository.findOne({
            where: {
                id: userId,
            },
            relations:{
                profile: true,
            }
            // relations: ['profile'],
        });
    }

    async create(user: User) {
        const userTemp = await this.userRepository.create(user);
        const saveResult = await this.userRepository.save(userTemp);
        return saveResult;
    }
    update(userId: number, user: Partial<User>) {
        return this.userRepository.update(userId, user);
    }
    remove(userId: number) {
        return this.userRepository.delete(userId);
    }


    getUser(): any {
        return {
            name: 'longfu sun111',
            age: 25,
        }
    }

    addUser(): any {
        return {
            code: 200,
            msg: 'add user success',
            data: {},
        }
    }

    async findUserLogs(userId: number) { 
        const user = await this.findById(userId);
        console.log(user);
        return this.logsRepository.find({
            where: {
                user: {
                    id: userId
                },
            },
            relations: {
                user: true,
            }
        });
    }
}
